﻿using System;
using System.Data;
using TopBuilds.Database;

namespace TopBuilds.Restaurant.Data
{
    public class EmployeeObject
    {
        public static Employee[] GetEmployees()
        {
            return Query(null);
        }

        public static Employee GetEmployee(int id)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(EmployeeDesc.Id, ConditionExpress.EQUALS, id);

            Employee[] set = Query( builder.ConditionString );
            if (set != null && set.Length > 0)
            {
                return set[0];
            }

            return null;
        }

        public static Employee GetEmployee(string name)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(EmployeeDesc.Name, ConditionExpress.EQUALS, name);

            Employee[] set = Query(builder.ConditionString);
            if (set != null && set.Length > 0)
            {
                return set[0];
            }

            return null;
        }


        public static Employee[] Query(string condition)
        {
            // query factories from database
            string sql = "select * from " + EmployeeDesc.TableName;
            if (condition != null && condition.Length > 0)
            {
                sql += " where ";
            }

            DataTable table = DatabaseObject.Instance.ExecuteQuerySQL(sql);

            if (table != null && table.Rows.Count > 0)
            {
                // rebuild the data
                Employee[] employees = new Employee[table.Rows.Count];

                for (int i = 0; i < table.Rows.Count; i++)
                {
                    Employee employee = new Employee();

                    employee.Id                 = Convert.ToInt32(table.Rows[i][EmployeeDesc.Id]);
                    employee.PrivilegeId        = Convert.ToInt32(table.Rows[i][EmployeeDesc.PrivilegeId]);
                    employee.DepartmentId       = Convert.ToInt32(table.Rows[i][EmployeeDesc.DepartmentId]);
                    employee.Name               = Convert.ToString(table.Rows[i][EmployeeDesc.Name]);
                    employee.Password           = Convert.ToString(table.Rows[i][EmployeeDesc.Password]);
                    employee.LastLoginTime      = Convert.ToDateTime(table.Rows[i][EmployeeDesc.LastLoginTime]);
                    employee.LoginCount         = Convert.ToInt32(table.Rows[i][EmployeeDesc.LoginCount]);

                    employees.SetValue(employee, i);
                }

                return employees;
            }

            return null;
        }

        public static Int32 Add(Employee employee)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.InsertAdd(EmployeeDesc.PrivilegeId,            employee.PrivilegeId);
            builder.InsertAdd(EmployeeDesc.DepartmentId,           employee.DepartmentId);
            builder.InsertAdd(EmployeeDesc.Name,                   employee.Name);
            builder.InsertAdd(EmployeeDesc.Password,               employee.Password);
            builder.InsertAdd(EmployeeDesc.LastLoginTime,          employee.LastLoginTime);
            builder.InsertAdd(EmployeeDesc.LoginCount,             employee.LoginCount);
            builder.QueryTableName = EmployeeDesc.TableName;

            string sql = builder.InsertCommandLine;

            return DatabaseObject.Instance.ExecuteNonQuerySQL(sql);
        }

        public static bool Delete(Int32 id)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(EmployeeDesc.Id, ConditionExpress.EQUALS, id);
            builder.QueryTableName = EmployeeDesc.TableName;
            string sql = builder.DeleteCommandLine;

            return (DatabaseObject.Instance.ExecuteNonQuerySQL(sql) != -1);
        }

        public static Int32 Update(Employee employee)
        {
            SqlBuilder builder = new SqlBuilder();

            builder.UpdateAdd(EmployeeDesc.PrivilegeId,              employee.PrivilegeId);
            builder.UpdateAdd(EmployeeDesc.DepartmentId,             employee.DepartmentId);
            builder.UpdateAdd(EmployeeDesc.Name,                     employee.Name);
            builder.UpdateAdd(EmployeeDesc.Password,                 employee.Password);
            builder.UpdateAdd(EmployeeDesc.LastLoginTime,            employee.LastLoginTime);
            builder.UpdateAdd(EmployeeDesc.LoginCount,               employee.LoginCount);

            builder.ConditionAdd(EmployeeDesc.Id, ConditionExpress.EQUALS, employee.Id);
            builder.QueryTableName = EmployeeDesc.TableName;

            string sql = builder.UpdateCommandLine;

            return DatabaseObject.Instance.ExecuteNonQuerySQL(sql);
        }
    }
}
